﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using moose.extension;
using moose.irc;

namespace moose.irc.standard.actions
{
    /// <summary>
    /// Automates the registration process
    /// </summary>
    [CLSCompliant(true)]
    [Action(
        Name = "moose-std-cmd-connect",
        Description="Automates Registration Process, using stored parameters",
        Version="0.1",
        Author="Mike MacKenzie-Jamer<email:mikejamer@gmail.com>",
        Type=ActionTypes.Command)]
    [Trigger(TriggerText="REGISTER")]
    public sealed class RegisterCommand : IrcAction
    {
        public override IrcSessionStates valid_states()
        {
            return IrcSessionStates.Connected;
        }

        public override void action(IrcSession session, IrcMessage message)
        {

            //We know for a FACT, that some networks do not accept this, this is inadiquate.  
            //So I need to become more familiar with how the registration process works, I want
            //this to be solid before long...

            string[] args = message.Arguments;
            if (args.Length > 0)
            {
                if (args.Length != 3)
                {
                    write_error(session);
                    return;
                }
                else
                {
                    session.Settings_Session[IrcSessionSettings.NICKNAME] = args[0];
                    session.Settings_Session[IrcSessionSettings.USER_NAME] = args[1];
                    session.Settings_Session[IrcSessionSettings.REAL_NAME] = args[2];
                }
            }

            if ((!session.Settings_Session.ContainsKey(IrcSessionSettings.NICKNAME)) ||
                (!session.Settings_Session.ContainsKey(IrcSessionSettings.USER_NAME)) ||
                (!session.Settings_Session.ContainsKey(IrcSessionSettings.REAL_NAME)))
            {
                write_error(session);
            }

            session.send("NICK " + session.Settings_Session[IrcSessionSettings.NICKNAME]);
            session.send("USER " + session.Settings_Session[IrcSessionSettings.USER_NAME] + " 8 *  :" 
                + session.Settings_Session[IrcSessionSettings.REAL_NAME]);
            return;
        }

        private void write_error(IrcSession session)
        {
            session.notify_event(IrcEvent.createEchoEvent("-REGISTER- Unable to register, must register manually.."));
            session.notify_event(IrcEvent.createEchoEvent("-REGISTER- /NICK <desired nickname>"));
            session.notify_event(IrcEvent.createEchoEvent("-REGISTER- /USER <desired username> 8 * :<desird real name>"));
            return;
        }
    }
}
